﻿using DBUtil;
using Function;
using Model;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL
{
    public class OutOfSetDAL
    {
        DbHelperSQLP DbHelperSQL = new DbHelperSQLP(PubConstant.JHShop);

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ST_OutOfSet");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(OutOfSetEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ST_OutOfSet(");
            strSql.Append("ShopID,LogTime,OutOfItem,Solve,CutMoney,Note,CreateTime,OpID)");
            strSql.Append(" values (");
            strSql.Append("@ShopID,@LogTime,@OutOfItem,@Solve,@CutMoney,@Note,@CreateTime,@OpID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ShopID", SqlDbType.Int,4),
					new SqlParameter("@LogTime", SqlDbType.VarChar,10),
					new SqlParameter("@OutOfItem", SqlDbType.NVarChar,128),
					new SqlParameter("@Solve", SqlDbType.NText),
					new SqlParameter("@CutMoney", SqlDbType.Decimal,5),
					new SqlParameter("@Note", SqlDbType.NText),
					new SqlParameter("@CreateTime", SqlDbType.VarChar,19),
					new SqlParameter("@OpID", SqlDbType.Int,4)};
            parameters[0].Value = model.ShopID;
            parameters[1].Value = model.LogTime;
            parameters[2].Value = model.OutOfItem;
            parameters[3].Value = model.Solve;
            parameters[4].Value = model.CutMoney;
            parameters[5].Value = model.Note;
            parameters[6].Value = model.CreateTime;
            parameters[7].Value = model.OpID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(OutOfSetEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ST_OutOfSet set ");
            strSql.Append("ShopID=@ShopID,");
            strSql.Append("LogTime=@LogTime,");
            strSql.Append("OutOfItem=@OutOfItem,");
            strSql.Append("Solve=@Solve,");
            strSql.Append("CutMoney=@CutMoney,");
            strSql.Append("Note=@Note,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("OpID=@OpID");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@ShopID", SqlDbType.Int,4),
					new SqlParameter("@LogTime", SqlDbType.VarChar,10),
					new SqlParameter("@OutOfItem", SqlDbType.NVarChar,128),
					new SqlParameter("@Solve", SqlDbType.NText),
					new SqlParameter("@CutMoney", SqlDbType.Decimal,5),
					new SqlParameter("@Note", SqlDbType.NText),
					new SqlParameter("@CreateTime", SqlDbType.VarChar,19),
					new SqlParameter("@OpID", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.ShopID;
            parameters[1].Value = model.LogTime;
            parameters[2].Value = model.OutOfItem;
            parameters[3].Value = model.Solve;
            parameters[4].Value = model.CutMoney;
            parameters[5].Value = model.Note;
            parameters[6].Value = model.CreateTime;
            parameters[7].Value = model.OpID;
            parameters[8].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ST_OutOfSet ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ST_OutOfSet ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OutOfSetEntity GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,ShopID,LogTime,OutOfItem,Solve,CutMoney,Note,CreateTime,OpID from ST_OutOfSet ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            OutOfSetEntity model = new OutOfSetEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OutOfSetEntity DataRowToModel(DataRow row)
        {
            OutOfSetEntity model = new OutOfSetEntity();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["ShopID"] != null && row["ShopID"].ToString() != "")
                {
                    model.ShopID = int.Parse(row["ShopID"].ToString());
                }
                if (row["LogTime"] != null)
                {
                    model.LogTime = row["LogTime"].ToString();
                }
                if (row["OutOfItem"] != null)
                {
                    model.OutOfItem = row["OutOfItem"].ToString();
                }
                if (row["Solve"] != null)
                {
                    model.Solve = row["Solve"].ToString();
                }
                if (row["CutMoney"] != null && row["CutMoney"].ToString() != "")
                {
                    model.CutMoney = decimal.Parse(row["CutMoney"].ToString());
                }
                if (row["Note"] != null)
                {
                    model.Note = row["Note"].ToString();
                }
                if (row["CreateTime"] != null)
                {
                    model.CreateTime = row["CreateTime"].ToString();
                }
                if (row["OpID"] != null && row["OpID"].ToString() != "")
                {
                    model.OpID = int.Parse(row["OpID"].ToString());
                }
            }
            return model;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select[id],[ShopID] 	,(SELECT stitle FROM dbo.ST_Shop WHERE ID=ShopID) AS shopName,[LogTime],[OutOfItem],[Solve],[CutMoney],[Note],[CreateTime],[OpID] 	,(SELECT TrueName FROM Permission.[User] WHERE UserID=[OpID]) AS userName ");
            strSql.Append(" FROM ST_OutOfSet ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}
